<?php
// $Id: deletebulk.php, v 1.0.0.0 2008/07/17 deanthayer Exp $
// based on: delete.php,v 1.25.6.2 2007/02/28 05:36:13 nicolasconnault
// Admin-only code to delete all courses in a category
require_once ("../config.php");
require_once ("../lib/xmlize.php");
require_once ("../lib/datalib.php");
require_once ("lib.php");


$termcat = required_param('termcat', PARAM_INT);

require_login();

$coursestodelete = get_courses($termcat, false, "c.id,c.shortname");

print_header("$site->shortname: $strdeletingcourse", $site->fullname, "<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> ");

if (is_array($coursestodelete) && count($coursestodelete) > 0) {
	foreach ($coursestodelete as $coursetodelete) {
		deletecourse($coursetodelete->id);
	}
} else {
	deletecourse($coursetodelete->id);
}
fix_course_sortorder(); //update course count in catagories

print_continue("category.php?id=$termcat");

print_footer();

function deletecourse($id) {
	if (!can_delete_course($id)) {
		error('You do not have the permission to delete this course.');
	}

	if (!$site = get_site()) {
		error("Site not found!");
	}

	$strdeletecourse = get_string("deletecourse");
	$stradministration = get_string("administration");
	$strcategories = get_string("categories");

	if (!$course = get_record("course", "id", $id)) {
		error("Course ID was incorrect (can't find it)");
	}

	$category = get_record("course_categories", "id", $termcat);

	// OK checks done, delete the course now.

	add_to_log(SITEID, "course", "delete", "view.php?id=$course->id", "$course->fullname (ID $course->id)");

	print_heading(get_string("deletedcourse", "", format_string($course->shortname)));
	delete_course($course->id);
}
?>
